package common;

import javax.servlet.http.HttpServletRequest;

import org.extremecomponents.table.context.Context;
import org.extremecomponents.table.context.HttpServletRequestContext;
import org.extremecomponents.table.limit.Limit;
import org.extremecomponents.table.limit.LimitFactory;
import org.extremecomponents.table.limit.TableLimit;
import org.extremecomponents.table.limit.TableLimitFactory;

/**
 * 分页信息类
 */
public class PageInfo {
	private HttpServletRequest request;
	
	private Limit limit;
	/** 每页记录数 */
	private int pageSize = 15;
	/** 总记录数 */
	private int totalSize = 0;
	/** 第几页 */
	private int pageNO = 0;

	public PageInfo(HttpServletRequest request) {
		this.request = request;
		init(request);
	}

	private void init(HttpServletRequest request) {
		Context context = new HttpServletRequestContext(request);
		LimitFactory limitFactory = new TableLimitFactory(context);
		this.limit = new TableLimit(limitFactory);

		if (request != null) {
			int ps = limit.getCurrentRowsDisplayed();
			if(ps == 0){
				String ecRows = request.getParameter("ec_crd");
				if(ecRows != null){
					ps = Integer.valueOf(ecRows);
				} 
				if(ps == 0){
					ecRows = request.getParameter("ec_rd");
					if(ecRows != null){
						ps = Integer.valueOf(ecRows);
					}
				}
				if(ps == 0){
					ps = 15;
				}
			}
			this.pageSize = ps;
			this.pageNO = limit.getPage() - 1;
		}
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public int getTotalSize() {
		return totalSize;
	}

	public void setTotalSize(int totalSize) {
		this.totalSize = totalSize;
		this.request.setAttribute("totalRows", new Integer(totalSize));
		this.limit.setRowAttributes(this.totalSize, this.pageSize);
	}

	public int getPageNO() {
		return pageNO;
	}

	public void setPageNO(int pageNO) {
		this.pageNO = pageNO;
	}

}
